package com.androidjam.runningacoruna;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class TrackPathOverlay extends Overlay implements Overlay.Snappable {

	private List<GeoPoint> geoPoints = null;

	@Override
	public void draw(Canvas canvas, MapView mapV, boolean shadow) {			
		Point from = new Point();
		Point to = new Point();
		
		Rect mandoRect = new Rect();        	
    	
    	mandoRect.set(0, 0, 160, 150);
    	
    	Paint mandosPaint = new Paint();
    	mandosPaint.setColor(Color.BLUE);    	
    	mandosPaint.setStyle(Style.FILL_AND_STROKE);        	            
    	mandosPaint.setAntiAlias(true);
    	mandosPaint.setAlpha(25);
    	canvas.drawRect(mandoRect, mandosPaint);            	                
    	
    	Paint qrletter = new Paint(); 
    	qrletter.setStyle(Paint.Style.FILL_AND_STROKE);
    	qrletter.setStrokeWidth(2);
    	qrletter.setTextSize(22);
    	qrletter.setColor(Color.WHITE);
        canvas.drawText(MainActivity.resources.getString(R.string.overlay_legend_title), 1, 16 , qrletter);

        qrletter.setColor(Color.GREEN);
        canvas.drawText(MainActivity.resources.getString(R.string.type_string_easy), 1, 50 , qrletter);
        qrletter.setColor(Color.YELLOW);
        canvas.drawText(MainActivity.resources.getString(R.string.type_string_medium), 1, 80 , qrletter);
        qrletter.setColor(Color.RED);
        canvas.drawText(MainActivity.resources.getString(R.string.type_string_hard), 1, 110 , qrletter);
        qrletter.setColor(Color.BLACK);
        canvas.drawText(MainActivity.resources.getString(R.string.type_string_ironman), 1, 140 , qrletter);
		
		if (geoPoints != null) {
			if (shadow) {						
				Projection projection = mapV.getProjection();
				// Creating geopoints - ommited for readability
				Path p = new Path();
				for (int i = 0; i < geoPoints.size(); i++) {
					if (i == geoPoints.size() - 1) {
						break;
					}
					from = new Point();
					to = new Point();
					projection.toPixels(geoPoints.get(i), from);
					projection.toPixels(geoPoints.get(i + 1), to);
					p.moveTo(from.x, from.y);															 				    				
					p.lineTo(to.x, to.y);
					//Log.i("TRACKPATHOVERLAY", "#################################PINTO PATH :"+geoPoints.size());
				}
				p.moveTo(from.x, from.y);					
				Paint mPaint = new Paint();
				mPaint.setStrokeWidth(2); 
				mPaint.setStyle(Style.STROKE);
				mPaint.setColor(Color.BLACK);
				mPaint.setAntiAlias(true);
				canvas.drawPath(p, mPaint);								
				
		
			}
		}
		
		super.draw(canvas, mapV, shadow);
		
	}

	public boolean onSnapToItem(int arg0, int arg1, Point arg2, MapView arg3) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void setGeoPoints(List<GeoPoint> geopoints){
		this.geoPoints = geopoints;
	}

}
